package com.community.web.master.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.community.model.entity.Charge;
import com.community.model.entity.Goods;
import com.community.model.entity.GoodsCategory;
import com.community.web.master.domain.dto.GoodsDto;
import com.community.web.master.domain.dto.PageGoodsDto;
import com.community.web.master.domain.vo.ChargeWithUserVo;
import com.community.web.master.domain.vo.GoodsWithCategoryNameVo;
import com.community.web.master.domain.vo.PageResult;
import com.community.web.master.exception.DataException;
import com.community.web.master.mapper.GoodsCategoryMapper;
import com.community.web.master.mapper.GoodsMapper;
import com.community.web.master.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李晶
 * @since 2024-07-02
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    /**
     * 分页查询
     * @param pageGoodsDto
     * @return
     */
    @Override
    public PageResult pageQuery(PageGoodsDto pageGoodsDto) {
        QueryWrapper<Goods> queryWrapper= new QueryWrapper<Goods>();
        if(pageGoodsDto.getKeyValue() !=null){
            if(!pageGoodsDto.getKeyValue().equals("")){
                queryWrapper.like("goods_name",pageGoodsDto.getKeyValue());
            }
        }
        if(pageGoodsDto.getCategoryId()!=null){
            if(pageGoodsDto.getCategoryId()==0) throw new DataException("信息有误");
            queryWrapper.eq("category_id",pageGoodsDto.getCategoryId());
        }
        Long total = goodsMapper.selectCount(queryWrapper);

        Map<String,Object> map=new HashMap<>();
        map.put("limit",(pageGoodsDto.getPageSize()));
        map.put("offset",(pageGoodsDto.getPage()-1)*pageGoodsDto.getPageSize());
        map.put("goods_name",pageGoodsDto.getKeyValue());
        map.put("category_id",pageGoodsDto.getCategoryId());

        List<GoodsWithCategoryNameVo> result= goodsMapper.selectPageByMap(map);

        return new PageResult(total,result);

    }

    /**
     * 添加商品
     * @param goodsDto
     */
    @Override
    public void add(GoodsDto goodsDto) {
        if(goodsDto.getCategoryId()==0) throw new DataException("数据有误");
        GoodsCategory goodsCategory = goodsCategoryMapper.selectById(goodsDto.getCategoryId());
        if(goodsCategory.getCategoryType()!=3){
            throw new DataException("商品只能添加在三级商品类别下");
        }
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsDto,goods);
        //生成随机goods_no
        UUID uuid = UUID.randomUUID();
        String randomUUIDString = uuid.toString().replace("-", "");
        goods.setGoodsNo(randomUUIDString);

        goodsMapper.insert(goods);

    }

    /**
     * 更新商品
     * @param goodsDto
     */
    @Override
    public void updateGoods(GoodsDto goodsDto) {
        if(goodsDto.getCategoryId()==0) throw new DataException("数据有误");
        GoodsCategory goodsCategory = goodsCategoryMapper.selectById(goodsDto.getCategoryId());
        if(goodsCategory.getCategoryType()!=3){
            throw new DataException("商品只能添加在三级商品类别下");
        }
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsDto,goods);
        goods.setGoodsNo(null);
        goodsMapper.updateById(goods);
    }

    @Override
    public GoodsWithCategoryNameVo getGoodsById(Integer id) {
        GoodsWithCategoryNameVo result = goodsMapper.getGoodsById(id);
        return result;
    }

    @Override
    public List<GoodsWithCategoryNameVo> getAllGoodsWithCategoryName() {
        List<GoodsWithCategoryNameVo> result = goodsMapper.getAllGoods();
        return result;
    }
}
